$(function () {
    $("#myGrid").datagrid({
        fit:true,
        fitColumns:true,
        columns:[
            [
                {field:'id',title:'分类编号',hidden:true},
                {field:'ordinal',title:'分类序号',width:100},
                {field:'name',title:'分类名称',width:100},
                {field:'parent',title:'上级分类',width:100,formatter:function (value) {
                    return value ? value.name:'';
                }},

            ]
        ],
        rownumbers:true,
        singleSelect:true,
        toolbar:'#tb'
    });
    $("#to_myGrid").datagrid({
        url:'/productCategory/list',
        fit:true,
        fitColumns:true,
        columns:[
            [
                {field:'name',title:'一级分类',width:100},

            ]
        ],
        singleSelect:true,
        rownumbers:true,
        onClickRow:function (index, row) {
            $("#myGrid").datagrid({
                url:'/productCategory/query?parentId='+row.id,
                fit:true,
                fitColumns:true,
                columns:[
                    [
                        {field:'id',title:'分类编号',width:100},
                        {field:'name',title:'分类名称',width:100},
                        {field:'parent',title:'上级分类',width:100,formatter:function (value) {
                            return value ? value.name:'';
                        }},

                    ]
                ],
                rownumbers:true,
                singleSelect:true,
                toolbar:'#tb'
            });
        }
    })
})
$(function () {
    $("#myDialog").dialog({
        width:280,
        buttons:'#form_btn',
        closed:true,
        onClose:function () {
            $("#myForm").form('clear')
        }
    })
})
function add() {
    $("#myDialog").dialog('open');
    $("#myDialog").dialog('setTitle','商品新增');
    $("#password_tr").show();
}
function save() {
    //默认是保存请求
    var url = "productCategory/save";
    var id= $("#productCategoryId").val();
    if (id) {
        //回显的productId有值则是update
        url = "productCategory/update";
    }
    $("#myForm").form("submit",{
        url:url,
        success:function (data) {
            data = $.parseJSON(data);
            if(data.success){
                $.messager.alert("温馨提示",data.errorMsg,'info',function () {
                    cancel();
                    reload();
                })
            }else{
                $.messager.alert("温馨提示",data.errorMsg,'warning')
            }
        }
    })
}
function cancel() {
    $("#myDialog").dialog('close');
}
function reload() {
    $("#myGrid").datagrid('reload');
}
function edit() {
    var row = $("#myGrid").datagrid('getSelected');
    if(!row){
        $.messager.alert('温馨提示','请选择一条数据','warning');
        return;
    }
    if(row.parent){
        row["parent.id"] = row.parent.id;
    }
    $("#myForm").form('load',row);

    $("#myDialog").dialog('open');
    $("#myDialog").dialog('setTitle','编辑部门');
}
function changeState() {
    var row = $("#myGrid").datagrid('getSelected');
    if(!row){
        $.messager.alert('温馨提示','请选择一条数据','warning');
        return;
    }
    $.messager.confirm("确认对话框","你确定要执行操作吗?",function (r) {
        if(r){
            $.get('/productCategory/delete',{id:row.id},function (data) {
                if(data.success){
                    $.messager.alert("温馨提示","删除成功",'info',function () {
                        cancel();
                        reload();
                    })
                }else{
                    $.messager.alert("温馨提示",data.errorMsg,'warning')
                }
            })
        }
    })
}